//
//  UICollectionViewHelper.swift
//  DictionaryModule
//
//  Created by xianquan on 2023/12/12.
//

import Foundation
import UIKit

public protocol IdentifierProvider {
    static var identifier: String { get }
}

public extension IdentifierProvider{
   static var identifier: String {
       "\(Self.self)"
   }
}


extension UICollectionReusableView: IdentifierProvider{}

public extension UICollectionView {
    
    func xw_register<T: UICollectionViewCell>(_ cellClass: T.Type, identifier: String = T.identifier){
        register(T.self, forCellWithReuseIdentifier: identifier)
    }
    
    func xw_dequeueReusableCell<T: UICollectionViewCell>(withReuseType type: T.Type, for indexPath: IndexPath, identifier: String = T.identifier) -> T{
        dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! T
    }
    
}

public extension UICollectionView {
    enum ReusableKind {
        case header
        case footer
        
        var kind: String {
            switch self {
            case .header:
                return UICollectionView.elementKindSectionHeader
            case .footer:
                return UICollectionView.elementKindSectionFooter
            }
        }
        public init(kindString: String){
            switch kindString{
            case UICollectionView.elementKindSectionFooter:
                self = .footer
            default:
                self = .header
            }
        }
    }
    func xw_register<T: UICollectionReusableView>(_ viewClass: T.Type, kind: ReusableKind, identifier: String = T.identifier){
        register(T.self, forSupplementaryViewOfKind: kind.kind, withReuseIdentifier: identifier)
    }
    
    func xw_dequeueReusableSupplementaryView<T: UICollectionReusableView>(_ viewClass: T.Type, kind: ReusableKind, indexPath: IndexPath, identifier: String = T.identifier) -> T {
        dequeueReusableSupplementaryView(ofKind: kind.kind, withReuseIdentifier: identifier, for: indexPath) as! T
    }
}
